package com.offcn.sellergoods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.offcn.dongyimaicommon.entity.PageResult;
import com.offcn.sellergoods.dao.BrandMapper;
import com.offcn.sellergoods.pojo.Brand;
import com.offcn.sellergoods.service.BrandService;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper,Brand> implements BrandService {

    @Override
    public List<Brand> findAll() {
        return this.list();
    }

    @Override
    public Brand findId(Long id) {
        return this.getById(id);
    }

    @Override
    public void add(Brand brand) {
        this.save(brand);
    }

    @Override
    public void update(Brand brand) {
        this.updateById(brand);
    }

    @Override
    public void remove(Long id) {
        this.removeById(id);
    }

    @Override
    public PageResult<Brand> pageList(Brand brand, int page, int size) {
        Page<Brand> myPage = new Page<>(page,size);
        QueryWrapper<Brand> wrapper = new QueryWrapper<>();
        if (null!= brand){
            if (null!=brand.getId()){
                wrapper.eq("id",brand.getId());
            }
            if (null!=brand.getName()){
                wrapper.like("name",brand.getName());
            }
            if (null!=brand.getFirstChar()){
                wrapper.eq("first_char",brand.getFirstChar());
            }
        }
        IPage<Brand> pages = this.page(myPage,wrapper);
        System.out.println(pages.getTotal());
        System.out.println(pages.getRecords());

        return new PageResult<Brand>(pages.getTotal(),pages.getRecords());
    }

}
